package collection.myList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import javax.swing.JSlider;
import objectpack.Student;
import org.springframework.util.StringUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
import util.MathUtil;

/**
 *
 *
 * list的add(index, element)不是替换，而是插入
 * @author
 */
public class ArrayList1 {


    @Test
    private void t9() {
        List<String> list = Arrays.asList(StringUtils.split("202", ","));
        Assert.assertTrue(list.contains("202"));
        System.out.println(list);

    }

    public static void main(String[] a) {

        List<Integer> numbs = new ArrayList<>(
                Arrays.asList(5,3,1));
        numbs.add(6);
        numbs.add(0,4);
        numbs.remove(1);

        List<Integer> list = List.of(9);
        // of返回的是不可变的集合，所以会有uoe错误
//        list.add(2);
//        list.remove(1);

        System.out.println(numbs);
//        System.out.println(list);
        for(ListIterator<Integer> i = numbs.listIterator(numbs.size()); i.hasPrevious();){
            System.out.print(i.previous().toString() + " ");
        }
        System.out.println();
        for(ListIterator<Integer> i = numbs.listIterator(); i.hasNext();){
            System.out.print(i.next().toString() + " ");
        }
        System.out.println();


        for (Iterator<Integer> i = numbs.iterator(); i.hasNext();){
            System.out.print(i.next() + " ");
        }

    }

    /**
     *
     */
    @Test
    public void copyList() {
        // list1和list2都是对象，=号的将list1的地址赋值给了list2
        List<String> list1 = new ArrayList<>();
        list1.add("a");

        List<String> list2 = list1;

        System.out.println(list2);

        list1.remove("a");
        System.out.println(list1);
        System.out.println(list2);

        list2.add("b");
        System.out.println(list1);
        System.out.println(list2);
    }

    @Test
    private void t3() {
        var list = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        list.add(0, 100);
        list.add(0, 1000);
        list.add(11, 100);
        System.out.println(list);

        var list2 = new ArrayList<Integer>();
        list2.addAll(list);
        System.out.println(list2);

    }

    @Test
    private void t5() {
        var list = new ArrayList<Student>();
        list.add(new Student("a", 0, "f"));
        list.add(new Student("a", 1, "f"));

        for (Student s : list) {
            if (s.getAge() == 1) {
                list.remove(s);
            }
        }

    }

    @Test
    private void t6() throws InterruptedException {
         Map map = new HashMap<>();

        for (int i = 0; i < 15; i++) {
            System.out.println("calculate");
            map.put(i, MathUtil.resolvePrime(987654321098765432L));
        }

        reverse(map);

//        Thread.sleep(1000 * 2);

        Runnable r = () -> {
            remove(map);
        };

        new Thread(r).start();

//        Runnable r = () -> {
//            for (int i = 0; i < 15; i++) {
//                System.out.println("calculate");
//                map.put(i * 10, MathUtil.resolvePrime(987654321098765432L));
//            }
//
//        };
//
//        new Thread(r).start();



//        Runnable r2 = () -> {
//            System.out.println("execute ");
////           map.keySet().forEach(k -> map.put(k, "k"));
//
////            map.keySet().forEach(k -> map.remove(k));
//
//            map.keySet().forEach(k -> map.remove(k));
//            map = null;
//            try {
//                Thread.sleep(100);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        };
//
//        new Thread(r2).start();



//        System.gc();
        // 如果在这里将map置空，需要将map改成final
//        map = null;

        Thread.sleep(1000 * 15);
    }

    private void reverse(Map<Integer, String> map) {
        Runnable r = () -> {
            for (int i = 0; i < 15; i++) {
                System.out.println("calculate");
                map.put(i * 10, MathUtil.resolvePrime(987654321098765432L));
            }

        };
        new Thread(r).start();
    }

    private void remove(Map<Integer, String> map) {

        map = null;
//        Runnable r2 = () -> {
//            System.out.println("execute ");
//            //           map.keySet().forEach(k -> map.put(k, "k"));
//
//            //            map.keySet().forEach(k -> map.remove(k));
//
////            map.keySet().forEach(k -> map.remove(k));
//            map = null;
//            try {
//                Thread.sleep(100);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        };
//
//        new Thread(r2).start();
    }

    @Test
    private void t8() {
        var list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);

        List<Integer> list1 = list.subList(0, 3);
        System.out.println(list1);
    }

    @Test
    private void t10() {
        List<String> list = List.of("a", "c", "d", "k");
        List<String> list1 = list.subList(0, 0);
        List<String> list2 = list.subList(0, 1);

        System.out.println(list1);
        System.out.println(list2);

    }





}
